// @ts-ignore
import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
// @ts-ignore
import type { App } from 'vue'

import Layout from '../layout/index.vue'

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    name: 'Home',
    meta: { hidden: true },
    children: [
      {
        path: '/home',
        component: () => import('../views/home.vue'),
        name: 'home',
        meta: { hidden: true  }
      },
      {
        path: '/sticky',
        component: () => import('../views/sticky.vue'),
        name: 'sticky',
        meta: { hidden: true  }
      },
      {
        path: '/cssVar',
        component: () => import('../views/cssVar.vue'),
        name: 'cssVar',
        meta: { hidden: true  }
      },
      {
        path: '/baseApi',
        component: () => import('../views/baseApi.vue'),
        name: 'baseApi',
        meta: { hidden: true  }
      },
      {
        path: '/hooks',
        component: () => import('../views/hooks.vue'),
        name: 'hooks',
        meta: { hidden: true  }
      },
      {
        path: '/pinia',
        component: () => import('../views/pinia.vue'),
        name: 'pinia',
        meta: { hidden: true  }
      },
      {
        path: '/router',
        component: () => import('../views/router.vue'),
        name: 'router',
        meta: { hidden: true  }
      },
      {
        path: '/404',
        component: () => import('../views/404.vue'),
        name: 'NoFind',
        meta: { hidden: true  }
      }
    ]
  },

]


const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
